<template>
  <div class="busi-form-container">
    <el-dialog v-if="dialogFormVisible" v-el-drag-dialog :visible.sync="dialogFormVisible" :close-on-click-modal="false" width="800px" :title="title" :append-to-body="true">
      <dynamic-form :table-field-info="tableFieldInfo" :vo="vo" :can-edit="canEdit" />

      <div slot="footer" class="dialog-footer">
        <el-button v-for="(btn,key) in footerBtns()" :key="key" :disabled="doing" :plain="btn.btnType===null || btn.btnType===''" :icon="'el-icon-'+btn.btnIcon" :type="btn.btnType!==null?btn.btnType:''" @click="btn.btnClick">{{ btn.btnName }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
    components: {

    },
    props: {

    },
    data() {
        return {
            title: '资源访问权限',
            dialogFormVisible: false,
            canEdit: 1,
            doing: false,
            vo: { id: '', visitType: 0, passenger: { resRoles: '' }},
            resIds: [],
            tableFieldInfo: [{
                'defaultValue': '0',
                'elementCanEdit': '{{canEdit}}',
                'elementFormatter': '0：不限制\r\n1：指定角色\r\n',
                'elementType': 'radio',
                'emptyMessage': '请选择授权方式 ',
                'errorMessage': '',
                'formTdAlign': 'text-left',
                'formTdColspan': 0,
                'isRequired': 1,
                'passenger': {},
                'propComment': '授权方式 ',
                'propName': 'visitType',
                'propType': 'Short',
                'textFormatter': {}
            }],
            resEntityName: ''
        }
    },
    computed: {

    },
    created() {
        this.$bus.on('openResPermissionForm', this.openBusiform)
    },
    beforeDestroy() {
        this.$bus.off('openResPermissionForm', this.openBusiform)
    },
    methods: {
        openBusiform(resIds, visitType, resEntityName, permissionCode, orgCode, sysCode) {
            this.doing = false

            this.resIds = resIds
            if (permissionCode === undefined || permissionCode === null || permissionCode === '') permissionCode = 'VISIT'
            this.tableFieldInfo[0].textFormatter.resType = resEntityName
            this.tableFieldInfo[0].textFormatter.permissionCode = permissionCode
            this.vo.visitType = visitType
            this.vo.orgCode = orgCode
            this.vo.sysCode = sysCode
            this.vo.passenger.pcode = permissionCode
            this.resEntityName = resEntityName
            this.dialogFormVisible = true
        },
        async submitBusiform() {
            const _this = this
            if (_this.doing) {
                return
            }
            this.$confirm('确认要批量修改访问权限吗？', '确认操作', { type: 'warning' })
                .then(() => {
                    _this.doing = true

                    this.$http({
                        method: 'POST',
                        contentType: 'application/x-www-form-urlencoded',
                        data: this.resIds,
                        params: { 'resEntityName': this.resEntityName, 'visitType': this.vo.visitType, 'roleIds': this.vo.passenger.resRoles, 'permissionCode': this.vo.passenger.pcode },
                        url: 'base/resRolePermission/saveResPermissions'
                    }).then(res => {
                        if (res.code === 'success') {
                            _this.$message({
                                type: res.code,
                                message: res.message,
                                showClose: true
                            })
                            _this.closeDialog()

                            _this.$bus.emit('refreshListData')
                        } else {
                            _this.$alert(res.message, '操作提示', { type: 'error' })
                        }
                        _this.doing = false
                    })
                })
                .catch(err => { if (err !== 'cancel') { console.error(err) } })
        },
        closeDialog() {
            this.dialogFormVisible = false
            this.moduleInfo = {}
            this.vo = {}
            this.$vnode.context.showResPermission = false
        },
        footerBtns() {
            const footerBtns = []

            footerBtns.push({
                id: 'btn_close',
                btnName: '关 闭',
                btnIcon: 'close',
                btnClick: this.closeDialog,
                btnPermission: '',
                btnType: ''
            })

            footerBtns.push({
                id: 'btn_save',
                btnName: '保 存',
                btnIcon: 'check',
                btnClick: this.submitBusiform,
                btnPermission: 'saveObject',
                btnType: 'primary'
            })

            return footerBtns
        }
    }
}
</script>

